<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="700px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="名称" prop="fullName">
              <el-input
                v-model="dataForm.fullName"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="试题库" prop="folders">
              <el-select
                v-model="dataForm.folders"
                multiple
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in folderOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="题型" prop="questionTypeArray">
              <el-checkbox-group
                v-model="dataForm.questionTypeArray"
                size="small"
              >
                <el-checkbox
                  v-for="(item, index) in questionTypeArrayOptions"
                  :key="index"
                  :label="item.id"
                  :disabled="item.disabled"
                >
                  {{ item.fullName }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="指定范围：" prop="scopeType">
              <el-radio-group v-model="dataForm.scopeType" size="small">
                <el-radio
                  v-for="(item, index) in scopeTypeOptions"
                  :key="index"
                  :label="item.id"
                  :disabled="item.disabled"
                >
                  {{ item.fullName }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="人员范围"
              prop="scopeData"
              v-if="dataForm.scopeType == 'select'"
            >
              <FilterSelect
                width="500px"
                v-model="dataForm.scopeData"
              ></FilterSelect>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getInfo, Update, Create } from "@/views/exam/api/practice";
import { getSelector } from "@/views/exam/api/questionLibrary";
import FilterSelect from "@/components/Generator/components/filterSelect/index";
import QuestionSingleDialog from "@/views/exam/components/questionSingleDialog.vue";

export default {
  components: { FilterSelect, QuestionSingleDialog },
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      dataForm: {
        fullName: "",
        folders: "",
        questionTypeArray: ["selectTi", "multiple", "determine", "blank"],
        scopeType: "all",
        scopeData: [],
        description: "",
        enabledMark: 1,
      },
      rules: {
        fullName: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        folders: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        questionTypeArray: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        scopeType: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
      },
      folderOptions: [],
      questionTypeArrayOptions: [
        { fullName: "单选题", id: "selectTi" },
        { fullName: "多选题", id: "multiple" },
        { fullName: "判断题", id: "determine" },
        { fullName: "填空题", id: "blank" },
      ],
      scopeTypeOptions: [
        { fullName: "全公司", id: "all" },
        { fullName: "仅创建人", id: "creator" },
        { fullName: "指定人员", id: "select" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getfolderOptions();
  },
  mounted() {},
  methods: {
    getfolderOptions() {
      getSelector()
        .then((res) => {
          this.folderOptions = res.data.list;
        })
        .catch(() => {
          this.folderOptions = [];
        });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          getInfo(this.dataForm.id).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
          this.dataForm.enabledMark = 1;
          this.dataForm.folders = [];
          this.dataForm.questionTypeArray = [
            "selectTi",
            "multiple",
            "determine",
            "blank",
          ];
          this.dataForm.scopeType = "all";
          console.log(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          var _data = this.dataList();
          this.btnLoading = true;
          const formMethod = this.dataForm.id ? Update : Create;
          formMethod(_data)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refresh", true);
                },
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.folders = JSON.stringify(_data.folders);
      _data.questionTypeArray = JSON.stringify(_data.questionTypeArray);
      _data.scopeData = JSON.stringify(_data.scopeData);
      _data.enabledMark = parseInt(_data.enabledMark);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.folders = JSON.parse(_dataAll.folders);
      _dataAll.questionTypeArray = JSON.parse(_dataAll.questionTypeArray);
      _dataAll.scopeData = JSON.parse(_dataAll.scopeData);
      _dataAll.enabledMark = parseInt(_dataAll.enabledMark);
      this.dataForm = _dataAll;
    },
  },
};
</script>
